﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace USB2XXX
{
    class USB2K
    {
        //定义函数返回错误代码
        public const Int32 K_SUCCESS                  = (0);   //函数执行成功
        public const Int32 K_ERR_NOT_SUPPORT          = (-1);  //适配器不支持该函数
        public const Int32 K_ERR_USB_WRITE_FAIL       = (-2);  //USB写数据失败
        public const Int32 K_ERR_USB_READ_FAIL        = (-3);  //USB读数据失败
        public const Int32 K_ERR_CMD_FAIL             = (-4);  //命令执行失败
        public const Int32 K_ERR_SERVER_NO_RESPONSE   = (-5);  //ECU无响应
        public const Int32 K_ERR_RESPONSE             = (-6);  //ECU响应数据错误
        public const Int32 K_ERR_SEND_DATA            = (-7);  //数据发送出错
        public const Int32 K_ERR_CHECK_ERROR          = (-8);  //数据发送出错
        //定义初始化K的数据类型
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]  
        public struct K_MSG
        {
          public Byte Fmt;        //Format byte, K_MSG_FMT
          public Byte TgtAddr;    //This is the target address for the message. It may be a physical or a functional address.
          public Byte SrcAddr;    //Source address
          public Byte Len;        //This byte is provided if the length in the header byte Fmt.DataLen is set to 0.
          public Byte SId;        //Service Identification byte
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 254, ArraySubType = UnmanagedType.I1)]
          public Byte[] Data;  //depending on service
          public Byte CS;         //Checksum byte
        }
        //USB2K相关函数定义
        [DllImport("USB2XXX.dll")]
        public static extern Int32 K_Init(Int32 DevHandle, Byte Channel,Int32 BaudRate);
        [DllImport("USB2XXX.dll")]
        public static extern Int32 K_5BaudInit(Int32 DevHandle, Byte Channel,Byte Addr,Byte[] pKB1,Byte[] pKB2);
        [DllImport("USB2XXX.dll")]
        public static extern Int32 K_FastInit(Int32 DevHandle, Byte Channel,IntPtr pRequest,IntPtr pResponse,Byte InterByteTimeMs);
        [DllImport("USB2XXX.dll")]
        public static extern Int32 K_Request(Int32 DevHandle, Byte Channel,IntPtr pRequest,IntPtr pResponse,Byte InterByteTimeMs,Int32 ResponseTimeOutMs);
    }
}
